package com.xdtech.hotel.vo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.sql.Timestamp;

import javax.persistence.Column;

import org.codehaus.jettison.json.JSONObject;

import com.thoughtworks.xstream.XStream;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.hotel.model.DeviceLink;
import com.xdtech.hotel.protocol.vo.Ac;
import com.xdtech.hotel.protocol.vo.Component;
import com.xdtech.hotel.protocol.vo.Dimmer;
import com.xdtech.hotel.protocol.vo.Music;
import com.xdtech.hotel.protocol.vo.Node;
import com.xdtech.hotel.protocol.vo.Tv;
import com.xdtech.hotel.util.HotelConstants;

import com.xdtech.web.freemark.item.GridColumn;

/**
 * 
 * @author max.zheng
 * @create 2015-04-04 11:12:27
 * @since 1.0
 * @see
 */
public class DeviceLinkItem implements Serializable{
	private static final long serialVersionUID = 1L;
	private Long id;
	@GridColumn(title="设备名称",width=100)
	private String lid;
	@GridColumn(title="设备类型",width=100,dictionaryCode="COMPONENT_TYPE")
	private String ltype;
	@GridColumn(title="地址",width=50)
	private String addr; // ” : “0”, #可选参数：0-255，设备地址；
	@GridColumn(title="通道",width=50)
	private String channel; // ” : “0”, #可选参数：0-255,设备通道；
	
	private String component;
	
	private Component comp;

	public void setId(Long id) {
		this.id = id;
	}
	public Long getId() {
		return id;
	}
	
	public void setLid(String lid) {
		this.lid = lid;
	}
	public String getLid() {
		return lid;
	}
	
	public void setLtype(String ltype) {
		this.ltype = ltype;
	}
	public String getLtype() {
		return ltype;
	}
	
	
	
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	public String getChannel() {
		return channel;
	}
	public void setChannel(String channel) {
		this.channel = channel;
	}
	public String getComponent() {
		return component;
	}
	public void setComponent(String component) {
		this.component = component;
	}
	/**
	 * 根据model构建vo
	 * 
	 * @author max
	 * @return
	 */
	public static DeviceLinkItem createItem(DeviceLink deviceLink) {
		DeviceLinkItem deviceLinkItem = new DeviceLinkItem();
		BeanUtils.copyProperties(deviceLinkItem, deviceLink);
		String ltype = deviceLink.getLtype();
		//自定义属性设置填充
		Component component = null;
		if (HotelConstants.COMPONENT_MUSIC.equals(ltype))
		{
			//(String addr, String channel, String power, String volume,
//			String ch, String item)
			//音乐
			Music music = (Music) new XStream().fromXML(deviceLink.getComponent());
			component = music;
		} else if (HotelConstants.COMPONENT_TV.equals(ltype))
		{
			//电视
			//音乐
			Tv tv = (Tv) new XStream().fromXML(deviceLink.getComponent());
			component = tv;
		} else if (HotelConstants.COMPONENT_AC.equals(ltype))
		{
			//音乐
			Ac ac = (Ac) new XStream().fromXML(deviceLink.getComponent());
			component = ac;		
		}else if (HotelConstants.COMPONENT_DIMMER.equals(ltype))
		{
			//调灯
			Dimmer dimmer = (Dimmer) new XStream().fromXML(deviceLink.getComponent());
			component = dimmer;				
		}else if (HotelConstants.COMPONENT_NODE.equals(ltype))
		{
			//节点
			Node node = (Node) new XStream().fromXML(deviceLink.getComponent());
			component = node;		
		}
		deviceLinkItem.setComp(component);
		return deviceLinkItem;
	}
	/**
	 * 根据model集合创建vo集合
	 * 
	 * @author max
	 * @return
	 */
	public static List<DeviceLinkItem> createItems(List<DeviceLink> deviceLinks) {
		List<DeviceLinkItem> deviceLinkItems = new ArrayList<DeviceLinkItem>();
		for (DeviceLink deviceLink : deviceLinks) {
			deviceLinkItems.add(createItem(deviceLink));
		}
		return deviceLinkItems;
	}
	public Component getComp() {
		return comp;
	}
	public void setComp(Component comp) {
		this.comp = comp;
	}
	
}
